
using System;
using System.Data;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

using Abp.UI;
using Abp.AutoMapper;
using Abp.Extensions;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Application.Services.Dto;
using Abp.Linq.Extensions;
using Ixyu.Helper;
using Ixyu.Work;
using Ixyu.Work.Dtos;




namespace Ixyu.Work
{
    /// <summary>
    /// WorkItem应用层服务的接口实现方法  
    ///</summary>
//    [AbpAuthorize]
    public class WorkItemAppService : IxyuAppServiceBase
    {
        private readonly IRepository<WorkItem, int> _entityRepository;

        

        /// <summary>
        /// 构造函数 
        ///</summary>
        public WorkItemAppService(
        IRepository<WorkItem, int> entityRepository
        
        )
        {
            _entityRepository = entityRepository; 
            
        }

        public async Task<WorkItemEditDto> Add(string title)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            // var entity = ObjectMapper.Map <WorkItem>(input);
            var entity = new WorkItem();
            entity.Title = title;
            entity.Status = 0;
            entity.Level = 0;
            entity.Hour = 0;
            entity.Come = "0";
            entity.Project = "0";
            entity.Files = "";
            entity = await _entityRepository.InsertAsync(entity);
            return entity.MapTo<WorkItemEditDto>();
        }
        /// <summary>
        /// 获取WorkItem的分页列表信息
        ///</summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<PagedResultDto<WorkItemListDto>> GetPaged(GetWorkItemsInput input)
		{

		    var query = _entityRepository.GetAll().Where(input.FilterText);
			// TODO:根据传入的参数添加过滤条件
            

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			// var entityListDtos = ObjectMapper.Map<List<WorkItemListDto>>(entityList);
			var entityListDtos =entityList.MapTo<List<WorkItemListDto>>();

			return new PagedResultDto<WorkItemListDto>(count,entityListDtos);
		}


		/// <summary>
		/// 通过指定id获取WorkItemListDto信息
		/// </summary>
		 
		public async Task<WorkItemEditDto> GetById(int id)
		{
			var entity = await _entityRepository.GetAsync(id);

		    var reslut= entity.MapTo<WorkItemEditDto>();
		    reslut.FileList = entity.Files.ToFileList();
		    return reslut;
		}

		/// <summary>
		/// 获取编辑 WorkItem
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		


		/// <summary>
		/// 添加或者修改WorkItem的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		

		/// <summary>
		/// 编辑WorkItem
		/// </summary>
		
		public virtual async Task Update(WorkItemEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新

			var entity = await _entityRepository.GetAsync(input.Id.Value);
			input.MapTo(entity);
		    entity.Files = input.FileList.ToFiles();
			// ObjectMapper.Map(input, entity);
		    await _entityRepository.UpdateAsync(entity);
		}
        public virtual async Task Finish(WorkItemEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _entityRepository.GetAsync(input.Id.Value);
            entity.Status = 99;
            entity.Hour = input.Hour;
            await _entityRepository.UpdateAsync(entity);
        }


        /// <summary>
        /// 删除WorkItem信息的方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task Delete(EntityDto<int> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(input.Id);
		}



		/// <summary>
		/// 批量删除WorkItem的方法
		/// </summary>
		
		public async Task BatchDelete(List<int> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _entityRepository.DeleteAsync(s => input.Contains(s.Id));
		}


		/// <summary>
		/// 导出WorkItem为excel表,等待开发。
		/// </summary>
		/// <returns></returns>
		//public async Task<FileDto> GetToExcel()
		//{
		//	var users = await UserManager.Users.ToListAsync();
		//	var userListDtos = ObjectMapper.Map<List<UserListDto>>(users);
		//	await FillRoleNames(userListDtos);
		//	return _userListExcelExporter.ExportToFile(userListDtos);
		//}

    }
}


